const axios = require('axios').default;
const iconv = require('iconv-lite');
const cheerio = require("cheerio");
const fs = require('fs');
const path = require('path');
const delay = require('delay');

const domain = 'https://www.dygod.net';

//http://zhenglinglu.cn/pages/6b18b3/
let cookie_text = 'guardret=WAc=; Hm_lvt_f7abbae08c28b34b707daf8674110ce5=1710374217; __51cke__=; Hm_lpvt_f7abbae08c28b34b707daf8674110ce5=1710381621; __tins__21827803=%7B%22sid%22%3A%201710381621248%2C%20%22vd%22%3A%201%2C%20%22expires%22%3A%201710383421248%7D; __51laig__=24';

axios.defaults.withCredentials = true;
axios.defaults.headers.common['Cookie'] = cookie_text;
axios.defaults.timeout = 10000;

async function download(url) {
    let { data: html } = await axios({
        method: 'get',
        url: url,
        responseType: 'arraybuffer',
    });
    //gbk转utf8
    html = iconv.decode(Buffer.from(html), 'gbk');
    return html;
}

async function sleep(seconds = 10) {
    console.log('sleep', seconds, 'seconds');
    await delay(seconds * 1000);
}
async function one_page(url) {
    await sleep();
    console.log('fetching:', url);

    let html = await download(url);
    if (!html) return;

    let $ = cheerio.load(html, { decodeEntities: false });
    let total = $(".co_content8 a.ulink").length;

    if (!total) {
        return true;
    }

    console.log('get', total, 'items');

    let all_address = [];
    $(".co_content8 a.ulink").each(function (i, e) {
        let address = domain + $(e).attr('href');
        all_address.push(address);
    });

    let lists = [];
    for (let address of all_address) {
        await sleep();
        let html = await download(address);
        if (!html) return;

        let $$ = cheerio.load(html, { decodeEntities: false });
        let download_title = $$('div.title_all h1').text();
        let download_url = $$('a[href^="magnet:"]').eq(0).attr('href');

        if (download_title && download_url) {
            console.log('get one item:', download_title);
            lists.push([download_title, download_url]);
        }
    }

    await fs.promises.appendFile('result.txt', JSON.stringify(lists), { encoding: 'utf8' });
    return true;
}

async function start() {
    let urls = [
        "https://www.dygod.net/html/gndy/dyzz/index.html",
        "https://www.dygod.net/html/gndy/dyzz/index_2.html",
        "https://www.dygod.net/html/gndy/dyzz/index_3.html",
        "https://www.dygod.net/html/gndy/dyzz/index_4.html",
        "https://www.dygod.net/html/gndy/dyzz/index_5.html",
    ];

    try {
        //clear result.txt
        await fs.promises.writeFile('result.txt', '', { encoding: 'utf8' });
        for (let url of urls) {
            await one_page(url);
        }
        console.log('success');
    } catch (err) {
        console.error(err.toString());
    }


    process.exit();
}

start();